package com.testin.servlet;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TestingUrlServlet extends BaseServlet {
	private static final long serialVersionUID = 7422910046866929397L;



	/**
	 * 验证url
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String url = request.getParameter("url");
		request.setCharacterEncoding("utf-8");    
		int flag=0;//0是可以访问，1表示不能访问
		if(url.lastIndexOf("ftp")!=0){
	   	try   { 
    	/*	URL   ruteUrl   =   new   URL(url); 
    		URLConnection   ruteCon   =   ruteUrl.openConnection(); 
    		ruteCon.setAllowUserInteraction(true); 
    		ruteCon.connect(); 
    		ruteCon.setDefaultUseCaches(true); */
	   		URL   ruteUrl   =   new   URL(url);        	
			HttpURLConnection con = (HttpURLConnection) ruteUrl.openConnection();

			if("application/x-itunes-ipa".equals(con.getContentType())||"application/vnd.android.package-archive".equals(con.getContentType())){
				flag=0;
		    	}else{
		    		flag=1; 
		    	}
    		  } catch  
    		  (MalformedURLException e)   { 
    			flag=1; 
    		}
    		catch(java.io.IOException e2){
    			flag=1;
            	
            }
		}
	
        response.getWriter().print(flag);
	}
	
	

}
